package com.ww.learn.coroutine.context

import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

// 打印结果
//job1: start...
//job2: start...
//job2: end...
//main: cancel request...
//main: running...
//job1: end...
fun main() = runBlocking {
    // 启动一个协程来处理某种传入请求
    val request = launch {
        // 孵化两个子作业，其中一个通过GlobalScope启动
        launch(Job()) {
            println("job1: start...")
            delay(1000)
            println("job1: end...")
        }
        // 承袭父协程的上下文
        launch {
            delay(100)
            println("job2: start...")
            delay(100)
            println("job2: end...")
        }
    }
    delay(500)
    request.cancel()
    println("main: cancel request...")
    println("main: running...")
    delay(1000)
}